
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<div class="modal" id="addLineDiv" tabindex="-1" role="dialog" aria-hidden="true">

	<div class="modal-dialog" role="document">

		<div class="modal-content animated bounceInRight">

			<div class="modal-header">
				<h4 class="modal-title">资源行编辑</h4>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			</div>

			<div class="modal-body" style="padding-bottom: 20px;">
				<%-- 导入提示信息框 --%>
			    <c:if test="${hint!=null&&hint!=''}">
			   		<jsp:include page="../../common/alert/alert.jsp">
			   			<jsp:param value="${hint}" name="alertType"/>
			   			<jsp:param value="${alertMessage}" name="alertMessage"/>
			   		</jsp:include>
			    </c:if>
			
				<form id="cform">
					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>资源</label>
						<div class="col-sm-9">
							<select class="select2 form-control" name="resourceCode" id="resourceCode">
	                        	<option value="" selected="selected">请选择...</option>
	                        	<c:forEach items="${requestScope.prodResourceList}" var="prodResource">
	                        		<option value="${prodResource.resourceCode}" resourceUnit="${prodResource.resourceUnit}" costAmount="${prodResource.costAmount}">${prodResource.resourceName}</option>
	                        	</c:forEach>
	                        </select>
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>资源单位</label>
						<div class="col-sm-9">
							<select class="select2 form-control" name="resourceUnit" id="resourceUnit">
	                        	<option value="" selected="selected">请选择...</option>
	                        	<c:forEach items="${requestScope.materialUnitMap}" var="materialUnit">
	                        		<option value="${materialUnit.key}">${materialUnit.value}</option>
	                        	</c:forEach>
	                        </select>
						</div>
					</div>
					<div class="hr-line-dashed"></div>

					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>资源用量</label>
						<div class="col-sm-9">
							<input id="resourceQuantity" name="resourceQuantity" type="text" class="form-control" value="${requestScope.prodWorkProcedureResource.resourceQuantity}">
							<span class="help-block m-b-none" style="color: gray;">工作中心可用数量<span id="resourceAvailableQuantitySpan"></span></span>
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>标准成本费率</label>
						<div class="col-sm-9">
							<input id="costAmount" name="costAmount" type="text" class="form-control" value="${requestScope.prodWorkProcedureResource.costAmount}">
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label">摘要</label>
						<div class="col-sm-9">
							<input id="memo" name="memo" type="text" class="form-control" value="${requestScope.prodWorkProcedureResource.memo}">
						</div>
					</div>
					<div class="hr-line-dashed"></div>

					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>状态</label>
						<div class="col-sm-9">
							<select class="form-control m-b" name="status" id="status">
								<option value="Y" selected="selected">有效</option>
								<option value="N">无效</option>
							</select>
						</div>
					</div>
					<div class="hr-line-dashed"></div>

					<div class="form-group row m-b-none">
						<div class="col-sm-12 col-sm-offset-2 text-right">
							<button class="btn btn-white btn-lg" type="button"
								data-dismiss="modal">返回</button>
							&nbsp;
							<button
								class="ladda-button ladda-button-demo btn btn-primary btn-lg"
								data-style="expand-right">
								&nbsp;&nbsp;确定&nbsp;&nbsp;<i class="fa fa-check-square-o"></i>
							</button>
						</div>
					</div>
					
					<input type="hidden" id="procedureCode" name="procedureCode" value="${param.procedureCode}"> 
					<input type="hidden" id="procedureResourceId" name="procedureResourceId" value="${requestScope.prodWorkProcedureResource.procedureResourceId}"> 
					<input type="hidden" id="createdDate" name="createdDate" value="${requestScope.prodWorkProcedureResource.createdDate}"> 
					<input type="hidden" id="createdBy" name="createdBy" value="${requestScope.prodWorkProcedureResource.createdBy}">
				</form>
			</div>

		</div>

	</div>

</div>

<script>
	$(document).ready(function() {
		//初始化status
		if("${requestScope.prodWorkProcedureResource.status}"!=""){
			$("#status").val("${requestScope.prodWorkProcedureResource.status}");
		}
		//初始化resourceCode
		if("${requestScope.prodWorkProcedureResource.resourceCode}"!=""){
			$("#resourceCode").val("${requestScope.prodWorkProcedureResource.resourceCode}");
		}
		//初始化resourceUnit
		if("${requestScope.prodWorkProcedureResource.resourceUnit}"!=""){
			$("#resourceUnit").val("${requestScope.prodWorkProcedureResource.resourceUnit}");
		}
		
		//初始化工作中心资源可用数量
	  	getResourceAvailableQuantityAjax($("#workCenterCode").val(), $("#resourceCode").val());
		
		//切换资源选择效果
		$("#resourceCode").change(function(){
			$("#resourceUnit").val($(this).find("option:selected").attr("resourceUnit"));
			$("#costAmount").val($(this).find("option:selected").attr("costAmount"));
			getResourceAvailableQuantityAjax($("#workCenterCode").val(), $("#resourceCode").val());
		});
		
		
		//表单提交
		var l = $('.ladda-button-demo').ladda();

		l.click(function() {
			$("#cform").valid();
			//l.ladda('stop');
		});

		$("#cform").validate({
			rules : {
				resourceCode : {
					required : true,
				},
				resourceUnit : {
					required : true,
				},
				costAmount : {
					required : true,
					gtZero : true,
				},
				resourceQuantity : {
					required : true,
					gtZero : true,
				}
			},
			submitHandler: function(form) {
				var submitFlag = "Y";
				
				var resourceAvailableQuantity = parseFloat($("#resourceAvailableQuantitySpan").text());;
				if($("#resourceQuantity").val()>resourceAvailableQuantity){
					submitFlag = "N";
					redragonJS.alert("资源用量不能大于工作中心的分配数量");
				}
			
				if(submitFlag=="Y"){
					l.ladda('start');
					editLine();
				}
		    }
		});
		
	});
	
	//异步编辑行
	function editLine(){
		redragonJS.loading("tabDiv");
	
		$.ajax({
			type: "post",
			url: "web/prodWorkProcedureResource/editProdWorkProcedureResource",
			data: {"resourceCode": $("#resourceCode").val(), "resourceQuantity": $("#resourceQuantity").val(), "memo": $("#memo").val(), "resourceUnit": $("#resourceUnit").val(), "costAmount": $("#costAmount").val(),
				   "status": $("#status").val(), "procedureResourceId": $("#procedureResourceId").val(), "createdDate": $("#createdDate").val(), "createdBy": $("#createdBy").val(), 
				   "procedureCode": $("#procedureCode").val()},
			async: false,
			dataType: "html",
			cache: false,
			success: function(data){
				redragonJS.removeLoading("tabDiv");
				$('#addLineDiv').modal('hide');
				$('.ladda-button-demo').ladda('stop');
				
				var json = JSON.parse(data);
				if(json.result=="success"){
					getLineTab($("#procedureCode").val());
				}else{
					redragonJS.alert("编辑资源行错误");
				}
				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
	
	//获取工作中心资源可用数量
	function getResourceAvailableQuantityAjax(workCenterCode, resourceCode){
		$.ajax({
			type: "post",
			url: "web/prodWorkProcedureResource/getResourceAvailableQuantityAjax",
			data: {"workCenterCode": workCenterCode, "resourceCode": resourceCode},
			async: false,
			dataType: "json",
			cache: false,
			success: function(data){
				if(data!=""){
					$("#resourceAvailableQuantitySpan").text(data.resourceQuantity);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
</script>
